package warmachineroster.presentation.editpart;

import java.util.List;

import org.eclipse.draw2d.FlowLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.editpolicies.RootComponentEditPolicy;

import warmachineroster.Roster;
import warmachineroster.RosterElement;
import warmachineroster.presentation.policy.RoosterLayoutEditPolicy;

public class RosterEditPart extends WarmachineRosterAbstractGraphicalEditPart implements Adapter {

	private RectangleFigure f;
	
	protected IFigure createFigure() {
		f = new RectangleFigure();
		f.setMaximumSize(new Dimension(220, -1));
		MarginBorder border = new MarginBorder(5);
		
		f.setBorder(border );
		f.setOpaque(true);
		FlowLayout layout = new FlowLayout(){
			/**
			 * Provides the given child's preferred size.
			 * 
			 * @param child
			 *            the Figure whose preferred size needs to be calculated
			 * @param wHint
			 *            the width hint
			 * @param hHint
			 *            the height hint
			 * @return the child's preferred size
			 */
//			protected Dimension getChildSize(IFigure child, int wHint, int hHint) {
//				Dimension viewSize = (((FigureCanvas) getViewer().getControl()).getViewport().getSize());
//				Dimension result = new Dimension(child.getPreferredSize(wHint, hHint)); 
//				result.width = viewSize.width - 10;
//				return result;
//			}
			
		};
		layout.setHorizontal(false);
		layout.setStretchMinorAxis(true);
		layout.setMajorAlignment(FlowLayout.ALIGN_LEFTTOP);
		layout.setMinorAlignment(FlowLayout.ALIGN_CENTER);
		f.setLayoutManager(layout);
		return f;
	}

	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.CONTAINER_ROLE, new RootComponentEditPolicy());

		installEditPolicy(EditPolicy.LAYOUT_ROLE, new RoosterLayoutEditPolicy());
	}

	@Override
	protected List<RosterElement> getModelChildren() {
		return getRoster().getRosterUnits();
	}

	public Roster getRoster() {
		return (Roster) getModel();
	}

}
